package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ArticuloAreaResponsable;

public class ArticuloAreaResponsableDAOImpl extends JdbcDaoSupport implements IArticuloAreaResponsableDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    protected FindListByCriteriaQuery findListByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_ARTICULO_AREA_RESP_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ArticuloAreaResponsable articuloAreaResponsable) {
		deleteOperation.execute( articuloAreaResponsable  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ArticuloAreaResponsable findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ArticuloAreaResponsable articuloAreaResponsable) {
		insertOperation.execute( articuloAreaResponsable );
	}

	public void update(ArticuloAreaResponsable articuloAreaResponsable) {
		updateOperation.execute( articuloAreaResponsable );
	}

	public ArticuloAreaResponsable findByCriteria(ArticuloAreaResponsable  articuloAreaResponsable) {
		return findByCriteriaQuery.runQuery(articuloAreaResponsable);
	}

	public List load(ArticuloAreaResponsable  articuloAreaResponsable) {
		return findAllQuery.runQuery(articuloAreaResponsable);
	}

	public ArticuloAreaResponsable findByActiveState(ArticuloAreaResponsable  articuloAreaResponsable) {
		return findByActiveStateQuery.runQuery(articuloAreaResponsable);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findListByCriteriaQuery = new FindListByCriteriaQuery( getDataSource() );
    	//Deactivated operations
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_ARTICULO_AREA_RESP_T ("
				  + " ID, "				  
				  + " AREA_RESPONSABLE, "
				  + " IDENTIFICACION, "
				  + " ID_TIPO_IDENTIFICACION, "
				  + " PRIMER_NOMBRE, "
				  + " SEGUNDO_NOMBRE, "
				  + " PRIMER_APELLIDO, "
				  + " SEGUNDO_APELLIDO, "
				  + " OBSERVACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_ARTICULO_AREA_RESP_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(ArticuloAreaResponsable articuloAreaResponsable) {
			InsertOperation.this.update( new Object[]{
				articuloAreaResponsable.getAreaResponsable(),
				articuloAreaResponsable.getIdentificacion(),
				articuloAreaResponsable.getIdTipoIdentificacion(),
				articuloAreaResponsable.getPrimerNombre(),
				articuloAreaResponsable.getSegundoNombre(),
				articuloAreaResponsable.getPrimerApellido(),
				articuloAreaResponsable.getSegundoApellido(),
				articuloAreaResponsable.getObservacion(),
				articuloAreaResponsable.getEstado(),
				articuloAreaResponsable.getUsuarioCrea()
			});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_ARTICULO_AREA_RESP_T SET "
					  + " AREA_RESPONSABLE = ? ,"
					  + " IDENTIFICACION = ? ,"
					  + " ID_TIPO_IDENTIFICACION = ? ,"
					  + " PRIMER_NOMBRE = ? ,"
					  + " SEGUNDO_NOMBRE = ? ,"
					  + " PRIMER_APELLIDO = ? ,"
					  + " SEGUNDO_APELLIDO = ? ,"
					  + " OBSERVACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
			  	  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ArticuloAreaResponsable articuloAreaResponsable) {
			UpdateOperation.this.update( new Object[]{
				articuloAreaResponsable.getAreaResponsable(),
				articuloAreaResponsable.getIdentificacion(),
				articuloAreaResponsable.getIdTipoIdentificacion(),
				articuloAreaResponsable.getPrimerNombre(),
				articuloAreaResponsable.getSegundoNombre(),
				articuloAreaResponsable.getPrimerApellido(),
				articuloAreaResponsable.getSegundoApellido(),
				articuloAreaResponsable.getObservacion(),
				articuloAreaResponsable.getEstado(),
				articuloAreaResponsable.getUsuarioModifica(),
				articuloAreaResponsable.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_ARTICULO_AREA_RESP_T "
										+ WHERE;
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(ArticuloAreaResponsable articuloAreaResponsable) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_ARTICULO_AREA_RESP_T "
										+ WHERE + " 1=1 "
			  	  	  					+ " ID = ? ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
					new Long(id)
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArticuloAreaResponsable articuloAreaResponsable = new ArticuloAreaResponsable();
				articuloAreaResponsable.setId(new Long(rs.getLong("ID")));
				articuloAreaResponsable.setAreaResponsable(rs.getString("AREA_RESPONSABLE"));
				articuloAreaResponsable.setIdentificacion(rs.getString("IDENTIFICACION"));
				articuloAreaResponsable.setIdTipoIdentificacion(new Long(rs.getLong("ID_TIPO_IDENTIFICACION")));
				articuloAreaResponsable.setIdTipoIdentificacionNombre(rs.getString("TIPO_IDENTIFICACION"));
				articuloAreaResponsable.setPrimerNombre(rs.getString("PRIMER_NOMBRE"));
				articuloAreaResponsable.setSegundoNombre(rs.getString("SEGUNDO_NOMBRE"));
				articuloAreaResponsable.setPrimerApellido(rs.getString("PRIMER_APELLIDO"));
				articuloAreaResponsable.setSegundoApellido(rs.getString("SEGUNDO_APELLIDO"));
				articuloAreaResponsable.setObservacion(rs.getString("OBSERVACION"));
				articuloAreaResponsable.setEstado(rs.getString("ESTADO"));
				articuloAreaResponsable.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				articuloAreaResponsable.setFechaCrea(rs.getDate("FECHA_CREA"));		
				articuloAreaResponsable.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				articuloAreaResponsable.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return articuloAreaResponsable;
		}
    }

    // ---- BaseQueryOperationMasterListItems for Mapping ResultSet values --------------
	abstract class BaseQueryOperationMasterListItems extends MappingSqlQuery {
		public BaseQueryOperationMasterListItems(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArticuloAreaResponsable articuloAreaResponsable = new ArticuloAreaResponsable();
			articuloAreaResponsable.setId(new Long(rs.getLong("ID")));
			articuloAreaResponsable.setAreaResponsable(rs.getString("AREA_RESPONSABLE"));
			articuloAreaResponsable.setObservacion(rs.getString("OBSERVACION"));
			return articuloAreaResponsable;
		}
    }

	// ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " ID = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public ArticuloAreaResponsable runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ArticuloAreaResponsable) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND AREA_RESPONSABLE = ? "
			  	  	  					//+ " AND IDENTIFICACION = ? "
			  	  	  					//+ " AND ID_TIPO_IDENTIFICACION = ? "
			  	  	  					//+ " AND PRIMER_NOMBRE = ? "
			  	  	  					//+ " AND SEGUNDO_NOMBRE = ? "
			  	  	  					//+ " AND PRIMER_APELLIDO = ? "
			  	  	  					//+ " AND SEGUNDO_APELLIDO = ? "
			  	  	  					//+ " AND OBSERVACION = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ArticuloAreaResponsable runQuery(ArticuloAreaResponsable articuloAreaResponsable) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//articuloAreaResponsable.getAreaResponsable(),
				//articuloAreaResponsable.getIdentificacion(),
				//articuloAreaResponsable.getIdTipoIdentificacion(),
				//articuloAreaResponsable.getPrimerNombre(),
				//articuloAreaResponsable.getSegundoNombre(),
				//articuloAreaResponsable.getPrimerApellido(),
				//articuloAreaResponsable.getSegundoApellido(),
				//articuloAreaResponsable.getObservacion(),
				//articuloAreaResponsable.getEstado(),
				//articuloAreaResponsable.getUsuarioCrea(),
				//articuloAreaResponsable.getFechaCrea(),
				//articuloAreaResponsable.getUsuarioModifica(),
				//articuloAreaResponsable.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ArticuloAreaResponsable) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindListByCriteriaQuery ---------------------------------    
    public class FindListByCriteriaQuery extends BaseQueryOperationMasterListItems {
		private static final String sql = " SELECT "
										+ " ID, AREA_RESPONSABLE, OBSERVACION "
										+ " FROM GES_ARTICULO_AREA_RESP_T "
										+ " WHERE 1=1 "
			  	  	  					+ " AND ESTADO = ? "
										+ "";
    	public FindListByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ArticuloAreaResponsable articuloAreaResponsable) {
			List result = execute(new Object[] {
				articuloAreaResponsable.getEstado()
			});
			if ( result != null && result.size() > 0 ) {
        		return result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = " SELECT " 
						+ " 	A.ID, "
						+ " 	A.AREA_RESPONSABLE, "
						+ " 	A.IDENTIFICACION, "
						+ " 	A.ID_TIPO_IDENTIFICACION, "
						+ "     B.VRT_NOMBRE AS TIPO_IDENTIFICACION, "
						+ " 	A.PRIMER_NOMBRE, "
						+ " 	A.SEGUNDO_NOMBRE, "
						+ " 	A.PRIMER_APELLIDO, "
						+ " 	A.SEGUNDO_APELLIDO, "
						+ " 	A.OBSERVACION, "
						+ " 	A.ESTADO, "
						+ " 	A.USUARIO_CREA, "
						+ " 	A.FECHA_CREA, "
						+ " 	A.USUARIO_MODIFICA, "
						+ " 	A.FECHA_MODIFICA "
						+ " FROM "
						+ " 	GES_ARTICULO_AREA_RESP_T A "
						+ " 	INNER JOIN "
						+ " 	ADM_VALOR_TIPO_MAESTRA_T B"
						+ "     ON "
						+ "     A.ID_TIPO_IDENTIFICACION = B.ID_VALOR_TIPO "
						+ " WHERE " 
						+ " 	1=1 "
						+ " AND UPPER(A.AREA_RESPONSABLE) LIKE ? "
						+ " AND ESTADO LIKE ? ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ArticuloAreaResponsable articuloAreaResponsable) {
			List result =  execute(new Object[]{
					"%" + (articuloAreaResponsable.getAreaResponsable() != null && !articuloAreaResponsable.getAreaResponsable().equals("") ? articuloAreaResponsable.getAreaResponsable().toUpperCase() : "%") + "%",
					articuloAreaResponsable.getEstado()
				});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ArticuloAreaResponsable runQuery(ArticuloAreaResponsable articuloAreaResponsable) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ArticuloAreaResponsable) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
